package com.etlmaster.transformer.funcs;

import java.util.Map;

import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.trans.core.Line;
import com.etlmaster.executor.trans.core.LineTransProcessor;
import com.etlmaster.executor.utils.LogWriter;



/**
 * 行级处理 脱敏
 * @author wugf
 * @date 2016-04-29
 */
@StepInstruction("desensitization")
public class TransFieldDesensitization extends LineTransProcessor {

	private int start_position;
	private int end_position;

	public TransFieldDesensitization(String sourceTab,String sourceCol,String targetTab,String targetCol,String changeCode,Map paramStr,Map var){
		
		super( sourceTab,sourceCol,targetTab,targetCol,changeCode, paramStr,var);
	
	}

	private String changeChar(String sourceValue) {
		char[] source = sourceValue.toCharArray();
		if ( start_position < source.length && end_position < source.length) {
			for (int i = start_position; i < end_position; i++) {
				source[i] = '*';
			}
		}else{
			LogWriter.addLog("ERROR","输入参数不合法，不进行脱敏");
		}
		return String.valueOf(source);
	}

	public int getStart_position() {
		return start_position;
	}

	public void setStart_position(int start_position) {
		this.start_position = start_position;
	}

	public int getEnd_position() {
		return end_position;
	}

	public void setEnd_position(int end_position) {
		this.end_position = end_position;
	}

	@Override
	public void transField(Line line) {
		String sourceValue = line.getField(getFieldIndex(line));
		String targetValue=changeChar(sourceValue);
		line.replaceField(targetValue, getFieldIndex(line));

	}


}
